﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using WFinger.AccessDAL;
using System.Data;
using System.Data.OleDb;
using WFinger.DALProfile;
using WFinger.Common;
using WFinger.DALFactory;
using System.Data.Common;

namespace WFinger.AccessDAL
{
    public class Navigater : INavigater
    {

        public string getNavigater() 
        {
            StringBuilder sb = new StringBuilder();
            IDataReader idr = this.getClassList("0");
            sb.Append("      <ul class=\"sf-menu\">\n");
            sb.Append("<li><a href='/' onmouseover=\"this.src='/Templets/images/1-1.jpg';\" onmouseout=\"this.src='/Templets/images/1.jpg';\" title='首页'><img border=0 src='/Templets/images/1.jpg'>");
            sb.Append("</a></li>");
                //得到第一级类别
                while(idr.Read())
                {
                    


                    sb.Append("<li>\n");

                    if (idr["classename"].ToString().ToLower() == "case" || idr["classename"].ToString().ToLower() == "energysupply" || idr["classename"].ToString().ToLower() == "govsupport" || idr["classename"].ToString().ToLower() == "service" || idr["classename"].ToString().ToLower() == "news")
                    {
                        sb.Append("<a><img onmouseover=\"this.src='/Templets/images/" + idr["classename"].ToString() + "-1.jpg';\" onmouseout=\"this.src='/Templets/images/" + idr["classename"].ToString() + ".jpg';\" border=0 src='/Templets/images/" + idr["classename"].ToString() + ".jpg'></a>\n");
                    }
                    else
                    {

                        sb.Append("<a target=\"_blank\" href=\"http://"
                            + Public.readparamConfig("siteDomain")
                            + idr["dpath"].ToString() + "/"
                            + idr["clav"].ToString()
                            + "\"><img onmouseover=\"this.src='/Templets/images/" + idr["classename"].ToString() + "-1.jpg';\" onmouseout=\"this.src='/Templets/images/" + idr["classename"].ToString() + ".jpg';\" border=0 src='/Templets/images/" + idr["classename"].ToString() + ".jpg'></a>\n");
                    }
                        //
                    IDataReader idr2 = null;
                    switch (idr["classename"].ToString().ToLower())
                    {
                        case "solutions":
                            idr2=this.getNewsList(idr["classid"].ToString());
                        break;
                        case "products":
                            idr2 = this.getNewsList(idr["classid"].ToString());
                        break;
                        default:
                            idr2 = this.getClassList(idr["classid"].ToString());
                        break;
                    }

                    if (idr2!= null)
                    {
                        
                        //得到第二季类别
                        sb.Append("<ul>\n");
                        while(idr2.Read())
                        {
                            
                                sb.Append("<li>\n");
                                sb.Append("<a target='_blank' href=\"" + idr2["url"].ToString() + "\">" + idr2["cnname"].ToString() + "</a>\n");

                                //如果有的话，得到第三级的内容
                                //暂时不加


                                sb.Append("</li>\n");
                            
                        }
                        idr2.Close();
                        sb.Append("</ul>\n");
                    }
                    sb.Append("</li>\n");

                }
                idr.Close();

            sb.Append("</ul>\n");




            return sb.ToString();
        }
        public IDataReader getClassList(string parentid) 
        {
            string sqlstr = "select classid,savepath as dpath,classsaverule as clav,classcname as cnname,classename,IIf(isurl=0,savepath+'/'+classsaverule,urladdress) as url from fs_news_class where parentid=@parentid and navishowtf=1 and isrecyle=0  order by orderid desc";
            OleDbParameter[] param = new OleDbParameter[1];

            param[0] = new OleDbParameter("@parentid", OleDbType.VarChar);
            param[0].Value = parentid;
            DbDataReader dr=DbHelper.ExecuteReader(CommandType.Text,sqlstr,param);
            if (dr.HasRows)
            {
                return dr;
            }
            else 
            {
                return null;
            }
            
        }
        public DbDataReader getNewsList(string classid)
        {
            string sqlstr = "select classid,'/html'+'/'+savepath as dpath,filename+fileexname as clav,newstitle as cnname,iif(newstype=2,urladdress,'/html'+'/'+savepath+'/'+filename+fileexname) as url from fs_news where islock=0 and isrecyle=0 and siteid='0' and classid=@classid  order by orderid desc";
            OleDbParameter[] param = new OleDbParameter[1];

            param[0] = new OleDbParameter("@classid", OleDbType.VarChar);
            param[0].Value = classid;
            DbDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sqlstr, param);
            if (dr.HasRows)
            {
                return dr;
            }
            else
            {
                return null;
            }

        }
       
    }
}
